<?php
/**
 * Created by PhpStorm.
 * User: ASUS
 * Date: 2016/10/26
 * Time: 19:28
 */
function dbInit(){
    $link=mysqli_connect('localhost','root','');
if(!$link){
    die('连接数据库失败!'.mysqli_error());
}
mysqli_query($link,'set names utf8');
mysqli_query($link,'use itcast');
return link;
}

function query($link,$sql){
    if ($result=mysqli_query($link,$sql)){
        return $result;
    }else{
        echo 'SQL执行失败：<br>';
        echo '错误的SQL为：',$sql,'<br>';
        echo '错误的代码为：',mysqli_errno($link),'<br>';
        echo '错误的信息为：',mysqli_error($link),'<br>';
        die;
    }
}

function fetchAll($link,$sql){
    if($result=query($link,$sql)){
        $rows=array();
        while($row=mysql_fetch_array($result,MYSQLI_ASSOC)){
            $rows[]=$row;
        }
        mysqli_free_result($result);
        return $rows;
    }else{
        return false;
    }
}

function fetchRow($link,$sql) {
    if($result=query($link,$sql)){
        $row=mysqli_fetch_array($result,MYSQLI_ASSOC);
        return $row;
    }else{
        return false;
    }
}


function safeHandle($link,$data){
    $data=htmlspecialchars($data);
    $data=mysqli_real_escape_string($link,$data);
    return $data;
}